﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using DBUtility;
using MySql.Data.MySqlClient;

namespace DAL
{
    public class DPrivateCar
    {
        /// <summary>
        /// 是否存在手机号记录
        /// </summary>
        public bool Exists(string phoneNumber)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from tprivatecar");
            strSql.Append(" where PhoneNumber=@PhoneNumber");
            MySqlParameter[] parameters = {
					new MySqlParameter("@PhoneNumber", MySqlDbType.String)
          };
            parameters[0].Value = phoneNumber;

            return DbHelperMySQL.Exists(strSql.ToString(), parameters);
        }
         
        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(Model.PrivateCarInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into tprivatecar(");
            strSql.Append("PhoneNumber,IMEI,RegistTime,LastLoginTime,Signature)");
            strSql.Append(" values (");
            strSql.Append("@PhoneNumber,@IMEI,@RegistTime,@LastLoginTime,@Signature)");
            MySqlParameter[] parameters = {
					new MySqlParameter("@PhoneNumber", MySqlDbType.VarChar,15),
					new MySqlParameter("@IMEI", MySqlDbType.VarChar,16),
					new MySqlParameter("@RegistTime", MySqlDbType.VarChar,20),
                    new MySqlParameter("@LastLoginTime", MySqlDbType.VarChar,20),
                    new MySqlParameter("@Signature", MySqlDbType.VarChar,40)};
            parameters[0].Value = model.PhoneNumber;
            parameters[1].Value = model.IMEI;
            parameters[2].Value = model.RegistTime;
            parameters[3].Value = model.LastLoginTime;
            parameters[4].Value = "";
            int rows = DbHelperMySQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }           
            return false;           
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(string phoneNumber,DateTime lastLoginTime)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update tprivatecar set ");            
            strSql.Append("LastLoginTime=@LastLoginTime");
            strSql.Append(" where PhoneNumber=@PhoneNumber");
            MySqlParameter[] parameters = {				                     
                    new MySqlParameter("@LastLoginTime", MySqlDbType.VarChar,20),
                    new MySqlParameter("@PhoneNumber", MySqlDbType.VarChar,15)
					};            
            parameters[0].Value = lastLoginTime.ToString("yyyy-MM-dd HH:mm:ss");
            parameters[1].Value = phoneNumber;

            int rows = DbHelperMySQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Model.PrivateCarInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update tprivatecar set ");
            strSql.Append("PhoneNumber=@PhoneNumber,");
            strSql.Append("IMEI=@IMEI,");
            strSql.Append("RegistTime=@RegistTime,");
            strSql.Append("LastLoginTime=@LastLoginTime");
            strSql.Append(" where id=@id");
            MySqlParameter[] parameters = {
					new MySqlParameter("@PhoneNumber", MySqlDbType.VarChar,15),
					new MySqlParameter("@IMEI", MySqlDbType.VarChar,16),
					new MySqlParameter("@RegistTime", MySqlDbType.VarChar,20),
                    new MySqlParameter("@LastLoginTime", MySqlDbType.VarChar,20),
					new MySqlParameter("@id", MySqlDbType.Int32,10)};
            parameters[0].Value = model.PhoneNumber;
            parameters[1].Value = model.IMEI;
            parameters[2].Value = model.RegistTime;
            parameters[3].Value = model.LastLoginTime;
            parameters[4].Value = model.Id;

            int rows = DbHelperMySQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

       
        public bool DeleteById(int id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from tprivatecar ");
            strSql.Append(" where id=@id");
            MySqlParameter[] parameters = {
					new MySqlParameter("@id", MySqlDbType.Int32)
};
            parameters[0].Value = id;

            int rows = DbHelperMySQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public bool DeleteByPhoneNumber(string phoneNumber)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from tprivatecar ");
            strSql.Append(" where PhoneNumber=@PhoneNumber");
            MySqlParameter[] parameters = {
					new MySqlParameter("@PhoneNumber", MySqlDbType.VarChar,15)
             };
            parameters[0].Value = phoneNumber;

            int rows = DbHelperMySQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public bool DeleteByImei(string imei)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from tprivatecar ");
            strSql.Append(" where IMEI=@IMEI");
            MySqlParameter[] parameters = {
					new MySqlParameter("@IMEI", MySqlDbType.VarChar,16)
             };
            parameters[0].Value = imei;

            int rows = DbHelperMySQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public Model.PrivateCarInfo GetModel(string phoneNum)
        {

            //strSql.Append("PhoneNumber=@PhoneNumber,");
            //strSql.Append("IMEI=@IMEI,");
            //strSql.Append("RegistTime=@RegistTime,");
            //strSql.Append("LastLoginTime=@LastLoginTime"); 
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select id,PhoneNumber,IMEI,RegistTime,");
            strSql.Append("TaxiNo,TaxiType,DriverName,");
            strSql.Append("DriverGender,NickName,");
            strSql.Append("LastLoginTime,Signature from tprivatecar ");
            strSql.Append(" where PhoneNumber=@PhoneNumber");
           
            MySqlParameter[] parameters = {
					new MySqlParameter("@PhoneNumber", MySqlDbType.VarChar,15)
                     
            };
            parameters[0].Value = phoneNum;

            Model.PrivateCarInfo model = new Model.PrivateCarInfo();
            DataSet ds = DbHelperMySQL.Query(strSql.ToString(), parameters);
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["id"] != null && ds.Tables[0].Rows[0]["id"].ToString() != "")
                {
                    model.Id = int.Parse(ds.Tables[0].Rows[0]["id"].ToString());
                }
                if (ds.Tables[0].Rows[0]["PhoneNumber"] != null && ds.Tables[0].Rows[0]["PhoneNumber"].ToString() != "")
                {
                    model.PhoneNumber= ds.Tables[0].Rows[0]["PhoneNumber"].ToString();
                }
                if (ds.Tables[0].Rows[0]["IMEI"] != null && ds.Tables[0].Rows[0]["IMEI"].ToString() != "")
                {
                    model.IMEI = ds.Tables[0].Rows[0]["IMEI"].ToString();
                }
                if (ds.Tables[0].Rows[0]["RegistTime"] != null && ds.Tables[0].Rows[0]["RegistTime"].ToString() != "")
                {
                    model.RegistTime = ds.Tables[0].Rows[0]["RegistTime"].ToString();
                }
                if (ds.Tables[0].Rows[0]["LastLoginTime"] != null && ds.Tables[0].Rows[0]["LastLoginTime"].ToString() != "")
                {
                    model.LastLoginTime = ds.Tables[0].Rows[0]["LastLoginTime"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Signature"] != null && ds.Tables[0].Rows[0]["Signature"].ToString() != "")
                {
                    model.Signature = ds.Tables[0].Rows[0]["Signature"].ToString();
                }
                if (ds.Tables[0].Rows[0]["TaxiNo"] != null && ds.Tables[0].Rows[0]["TaxiNo"].ToString() != "")
                {
                    model.TaxiNo = ds.Tables[0].Rows[0]["TaxiNo"].ToString();
                }
                if (ds.Tables[0].Rows[0]["TaxiType"] != null && ds.Tables[0].Rows[0]["TaxiType"].ToString() != "")
                {
                    model.TaxiType = ds.Tables[0].Rows[0]["TaxiType"].ToString();
                }
                if (ds.Tables[0].Rows[0]["DriverName"] != null && ds.Tables[0].Rows[0]["DriverName"].ToString() != "")
                {
                    model.DriverName = ds.Tables[0].Rows[0]["DriverName"].ToString();
                }
                if (ds.Tables[0].Rows[0]["DriverGender"] != null && ds.Tables[0].Rows[0]["DriverGender"].ToString() != "")
                {
                    model.DriverGender =(byte)int.Parse(ds.Tables[0].Rows[0]["DriverGender"].ToString());
                }
                if (ds.Tables[0].Rows[0]["NickName"] != null && ds.Tables[0].Rows[0]["NickName"].ToString() != "")
                {
                    model.NickName = ds.Tables[0].Rows[0]["NickName"].ToString();
                }
                return model;
            }
             return null;            
        }
 
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select id,PhoneNumber,IMEI,RegistTime,");
            strSql.Append("LastLoginTime from tprivatecar ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperMySQL.Query(strSql.ToString());
        }

        public DataSet GetPhoneNumberList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select PhoneNumber  from tprivatecar ");  
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperMySQL.Query(strSql.ToString());
        }

        public bool UpdateSignatureByPhoneNumber(string phoneNumber,string signature)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update tprivatecar set ");
            strSql.Append("Signature=@Signature");            
            strSql.Append(" where PhoneNumber=@PhoneNumber");
            MySqlParameter[] parameters = {				
                    new MySqlParameter("@Signature", MySqlDbType.VarChar,20),
                    new MySqlParameter("@PhoneNumber", MySqlDbType.VarChar,15)
					};
            parameters[0].Value = signature;
            parameters[1].Value = phoneNumber;

            int rows = DbHelperMySQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public bool UpdateCarInfoByPhoneNumber(Model.PrivateCarInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update tprivatecar set ");
            strSql.Append("TaxiNo=@TaxiNo,TaxiType=@TaxiType,");
            strSql.Append("DriverName=@DriverName,DriverGender=@DriverGender");
            strSql.Append(" where PhoneNumber=@PhoneNumber");
            MySqlParameter[] parameters = {				
                    new MySqlParameter("@TaxiNo", MySqlDbType.VarChar,8),
                    new MySqlParameter("@TaxiType", MySqlDbType.VarChar,12),
                    new MySqlParameter("@DriverName", MySqlDbType.VarChar,5),
                    new MySqlParameter("@DriverGender", MySqlDbType.VarChar,2),
                    new MySqlParameter("@PhoneNumber", MySqlDbType.VarChar,15)
					};
            parameters[0].Value = model.TaxiNo;
            parameters[1].Value = model.TaxiType;
            parameters[2].Value = model.DriverName;
            parameters[3].Value = model.DriverGender;
            parameters[4].Value = model.PhoneNumber;
            int rows = DbHelperMySQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public DataSet GetListByPage(int page, int pagesize, string parameter, string parameterValue)
        {
            if (parameterValue == null)
            {
                return null;
            }
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  Id,PhoneNumber,IMEI,");
            strSql.Append("RegistTime,LastLoginTime,");
            strSql.Append("TaxiNo,TaxiType,DriverName,");
            strSql.Append("(case DriverGender when '0' then '男' ");
            strSql.Append("when '1' then '女' end) as DriverGender,");
            strSql.Append("Signature from tprivatecar ");            
            if (parameter.Trim() != "" && parameterValue != "")
            {
                strSql.Append("where  ");
                strSql.Append(parameter);
                strSql.Append(" like '%");
                strSql.Append(parameterValue);
                strSql.Append("%'");
            }
            strSql.Append(" order by LastLoginTime desc ");
            strSql.Append("limit ");
            strSql.Append((page - 1) * pagesize);
            strSql.Append(",");
            strSql.Append(pagesize);
            DataSet ds = DbHelperMySQL.Query(strSql.ToString());
            return ds;

        }

        public int GetCount(string parameter, string parameterValue)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(*) from tprivatecar ");
            if (parameter.Trim() != "" && parameterValue.Trim() != "")
            {
                strSql.Append("where ");
                strSql.Append(parameter);
                strSql.Append(" like '%");
                strSql.Append(parameterValue);
                strSql.Append("%'");
            }
            DataSet ds = DbHelperMySQL.Query(strSql.ToString());
            if (ds == null || ds.Tables.Count != 1 || ds.Tables[0].Rows.Count != 1)
            {
                return -1;
            }
            return int.Parse(ds.Tables[0].Rows[0][0].ToString());
        }

        public DataSet GetListWhere(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * from tprivatecar ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperMySQL.Query(strSql.ToString());
        }

    }
}
